package com.yonyou.crm.sys.queryplan.service;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.cum.customer.queryplan.CustomerQueryPlanHandle;
import com.yonyou.crm.cum.customer.queryplan.GroupCustomerQueryPlanHandle;
import com.yonyou.crm.sys.queryplan.entity.QueryPlanVO;
import com.yonyou.iuap.context.InvocationInfoProxy;
@Service
public class QueryPlanBizService extends BaseBizServiceImpl<QueryPlanVO>{
	
	@Autowired
	private GroupCustomerQueryPlanHandle gcHandle;
	@Autowired
	private CustomerQueryPlanHandle cHandle;

	public List<QueryPlanVO> getQueryPlanList(Map<String, Object> param) {
		param.put("tenantId", InvocationInfoProxy.getTenantid());
		param.put("roleType", InvocationInfoProxy.getParameter(LoginContextConstant.ROLETYPE));
		return dao.selectVoList(QueryPlanVO.class, "getQueryPlanList", param);
	}
	
	public Map<String, Object> handleSearchPlan(Map<String, Object> paramMap){
		// PC端根据查询方案查询客户
		if (paramMap != null && paramMap.containsKey("defClass")) {
			// PC端处理查询方案
			String className = (String) paramMap.get("defClass");
			paramMap.remove("defClass");
			try {
				Object service = getHandle(className);
				if (service != null) {
					Method method = service.getClass().getDeclaredMethod("handle", Map.class);
					@SuppressWarnings("unchecked")
					List<String> idList = (List<String>) method.invoke(service,paramMap);
					if (idList != null && idList.size() > 0) {
						paramMap.put("idList", idList);
					}
				}
				
			} catch (Exception e) {}
		}
		return paramMap;
	}
	
	private Object getHandle(String className) {
		
		Map<String, Object> serviceMap = new HashMap<String, Object>();
		serviceMap.put(GroupCustomerQueryPlanHandle.class.getName(), gcHandle);
		serviceMap.put(CustomerQueryPlanHandle.class.getName(), cHandle);
		return serviceMap.get(className);
	}
}
